// SMTradeCell.m
#import "SMTradeCell.h"
#import <Masonry/Masonry.h>
#import <SDWebImage/UIImageView+WebCache.h>
#import "../../Models/SMTradeItem.h"

@interface SMTradeCell ()
@property(nonatomic, strong) UIView *card;
@property(nonatomic, strong) UIImageView *thumb;
@property(nonatomic, strong) UILabel *titleL;
@property(nonatomic, strong) UILabel *descL;
@property(nonatomic, strong) UILabel *metaL;
@end

@implementation SMTradeCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = UIColor.systemGroupedBackgroundColor;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        _card = [UIView new];
        _card.backgroundColor = UIColor.secondarySystemBackgroundColor;
        _card.layer.cornerRadius = 12; _card.layer.masksToBounds = YES;
        [self.contentView addSubview:_card];
        [_card mas_makeConstraints:^(MASConstraintMaker *make){
            make.edges.equalTo(self.contentView).insets(UIEdgeInsetsMake(8, 12, 8, 12));
            make.height.mas_greaterThanOrEqualTo(96);
        }];
        _thumb = [UIImageView new];
        _thumb.contentMode = UIViewContentModeScaleAspectFill; _thumb.clipsToBounds=YES;
        _thumb.layer.cornerRadius = 8;
        [_card addSubview:_thumb];
        _titleL = [UILabel new]; _titleL.font=[UIFont systemFontOfSize:16 weight:UIFontWeightSemibold];
        _descL = [UILabel new]; _descL.font=[UIFont systemFontOfSize:14]; _descL.textColor=UIColor.secondaryLabelColor; _descL.numberOfLines=2;
        _metaL = [UILabel new]; _metaL.font=[UIFont systemFontOfSize:12]; _metaL.textColor=UIColor.tertiaryLabelColor;
        [_card addSubview:_titleL]; [_card addSubview:_descL]; [_card addSubview:_metaL];
        [_thumb mas_makeConstraints:^(MASConstraintMaker *make){
            make.left.top.equalTo(self.card).offset(12);
            make.bottom.equalTo(self.card).offset(-12);
            make.width.mas_equalTo(112);
        }];
        [_titleL mas_makeConstraints:^(MASConstraintMaker *make){
            make.top.equalTo(self.card).offset(12);
            make.left.equalTo(self.thumb.mas_right).offset(12);
            make.right.equalTo(self.card).offset(-12);
        }];
        [_descL mas_makeConstraints:^(MASConstraintMaker *make){
            make.top.equalTo(self.titleL.mas_bottom).offset(6);
            make.left.right.equalTo(self.titleL);
        }];
        [_metaL mas_makeConstraints:^(MASConstraintMaker *make){
            make.left.right.equalTo(self.titleL);
            make.bottom.equalTo(self.card).offset(-12);
        }];
        UIView *line = [UIView new];
        line.backgroundColor = [UIColor colorWithWhite:0 alpha:0.06];
        [self.contentView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make){
            make.left.equalTo(self.contentView).offset(12);
            make.right.equalTo(self.contentView).offset(-12);
            make.bottom.equalTo(self.contentView);
            make.height.mas_equalTo(0.5);
        }];
    }
    return self;
}
- (void)configure:(SMTradeItem *)item {
    NSString *url = item.imageURL ?: @"";
    UIImage *ph = nil;
    if (@available(iOS 13.0, *)) ph = [UIImage systemImageNamed:@"shippingbox"];
    [self.thumb sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:ph];
    self.titleL.text = item.title ?: @"标题";
    self.descL.text  = item.detail ?: @"简介描述";
    NSString *meta = @"";
    if (item.city.length) meta = [meta stringByAppendingFormat:@"%@  ", item.city];
    if (item.price.length) meta = [meta stringByAppendingFormat:@"¥%@  ", item.price];
    if (item.updatedAt) {
        NSDateFormatter *df = [NSDateFormatter new];
        df.dateFormat = @"MM-dd HH:mm";
        meta = [meta stringByAppendingFormat:@"%@", [df stringFromDate:item.updatedAt]];
    }
    self.metaL.text = meta;
}
@end
